//
//  OYVideoResponse.m
//  ObjectiveYouTube
//
//  Created by Tristan O'Tierney on 3/17/08.
//  Copyright 2008 Tristan O'Tierney. All rights reserved.
//

#import <ObjectiveYouTube/ObjectiveYouTube.h>

static NSString *kOYResponseElementName = @"id";
static NSString *kOYPublicURLBase = @"http://www.youtube.com/watch?v=%@";
static NSString *kOYEditURLBase = @"http://www.youtube.com/my_videos_edit?ns=1&amp;video_id=%@";

@implementation OYVideoResponse
#pragma mark -
#pragma mark Initializers
#pragma mark -

- (id)initWithXMLDocument:(NSXMLDocument *)doc {
	if ((self = [super init])) {
		_url = nil;
		
		NSXMLElement *root = [doc rootElement];
		NSArray *children = [root elementsForName:kOYResponseElementName];
		
		if (children && [children count]) {
			NSXMLNode *idNode = [children objectAtIndex:0];
			NSString *urlStr = [idNode stringValue];
			
			_url = [[NSURL alloc] initWithString:urlStr];
			_id = [[urlStr lastPathComponent] retain];
			_publicURL = [[NSURL alloc] initWithString:[NSString stringWithFormat:kOYPublicURLBase, _id]];
			_editURL = [[NSURL alloc] initWithString:[NSString stringWithFormat:kOYEditURLBase, _id]];
		}
		
		if (!_url) {
			[self release];
			self = nil;
		}
	}
	
	return self;
}

- (void)dealloc {
	[_url release];
	_url = nil;
	[_id release];
	_id = nil;
	[_publicURL release];
	_publicURL = nil;
	[_editURL release];
	_editURL = nil;
	
	[super dealloc];
}

#pragma mark -
#pragma mark Accessors
#pragma mark -

- (NSURL *)apiURL {
	return _url;
}

- (NSURL *)publicURL {
	return _publicURL;
}

- (NSURL *)editURL {
	return _editURL;
}

- (NSString *)videoID {
	return _id;
}
@end
